<?php
/**
 * Class Sql is used to connect and communicate with the database.
 */
class Sql {
	
	/**
	 * MySql link identifier.
	 *
	 * @var resource
	 */
	public $connection = NULL;
	
	/**
	 * Connects to the database and selects the database.
	 */
	public function connect() {
		$this->connection = @mysql_connect( MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD );
		mysql_select_db( MYSQL_DATABASE, $this->connection );
	}
	
	/**
	 * Performes a simple query from the database.
	 *
	 * @param String $query
	 * @return array $response
	 */
	public function query( $query ) {
		if( !$this->connection ) {
			$this->connect();
		}
		
		$response = mysql_query( $query, $this->connection );
		
		return $response;
	}
	
	/**
	 * Performes a query from the database and returns the data.
	 *
	 * @param String $query
	 * @return array $result
	 */
	public function selectQuery( $query ) {
		$response = Sql::query( $query ) or die(mysql_error());;
		
		$result = array();
		while( $row = mysql_fetch_array($response) ) {
			$result[] = $row;
		}
		return $result;
	}
	
	/**
	 * Returns the number of affected rows in previous MySql operation.
	 *
	 * @return int
	 */
	public function affectedRows() {
		return mysql_affected_rows( $this->connection );
	}
	
}
?>